package com.yyh.result;

import com.yyh.result.code.BaseResultCode;
import com.yyh.result.code.ResultCodeInterface;

/**
 * 统一的返回结果
 * @author xuan
 * @create 2021/12/11
 */
public class JsonResult<T>
{
    /** 状态代码 */
    private Integer code;
    /** 状态码描述 */
    private String message;
    /** 数据 */
    private T data;

    public JsonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public JsonResult(Integer code, String message, T data) {
        this(code, message);
        this.data = data;
    }

    public static <T> JsonResult<T> getSuccess(T data) {
        BaseResultCode success = BaseResultCode.SUCCESS;
        return new JsonResult<>(success.getCode(), success.getMessage(), data);
    }

    public static <T> JsonResult getFailure(String message) {
        BaseResultCode failure = BaseResultCode.FAILURE;
        return new JsonResult(failure.getCode(), message);
    }

    public static <T> JsonResult<T> getResult(ResultCodeInterface resultCode) {
        return new JsonResult<>(resultCode.getCode(), resultCode.getMessage());
    }

    public static <T> JsonResult<T> getResult(ResultCodeInterface resultCode, T data) {
        return new JsonResult<>(resultCode.getCode(), resultCode.getMessage(), data);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
